$import( "SmitearWebGL.Programs.AbstractProgram" );

SmitearWebGL.Programs.Hdr.VBlurProgram = SmitearWebGL.Programs.AbstractProgram.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Programs.Hdr.VBlurProgram" );
	}
	,
	
	/******** public method ********/
	
	activeUniform : function( renderableObject )
	{
		this.context.gl.uniformMatrix3fv( this._shader.uniforms.uVBloomOffsets.location, false, renderableObject._vBloomOffsets );
		this.context.gl.uniformMatrix3fv( this._shader.uniforms.uVBloomWeights.location, false, renderableObject._vBloomWeights );
		
		var texture = renderableObject.getTextureAt( 0 );
		this.context.gl.activeTexture( this.context.gl.TEXTURE0 );
		this.context.gl.bindTexture( this.context.gl.TEXTURE_2D, texture.getGlTexture() );
		this.context.gl.uniform1i( this._shader.uniforms.uSampler.location, 0 );
		
		return SmitearWebGL.Core.HResult.SUCCESS;
	}
	,
	
	/******** private method ********/
	
	_generateVertexShaderStr : function()
	{
		this._super();
		
		var vertStr =
		"\
		attribute vec3 " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +";\n\
		varying vec2 vTexCoord;\n\
		void main(void)\n\
		{\n\
			vec4 pos = vec4(" + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +", 1.0);\n\
			gl_Position = pos;\n\
			vTexCoord = vec2((pos.x + 1.0) * 0.5, (pos.y + 1.0) * 0.5);\n\
		}\n\
		";
		
		return vertStr;
	}
	,
	_generateFragmentShaderStr : function()
	{
		this._super();
		
		var fragStr = 
		"\
		#ifdef GL_ES\n\
		precision highp float;\n\
		#endif\n\
		varying vec2 vTexCoord;\n\
		uniform mat3 uVBloomOffsets;\n\
		uniform mat3 uVBloomWeights;\n\
		uniform sampler2D uSampler;\n\
		void main(void) {\n\
			vec4 color = vec4(0.0, 0.0, 0.0, 0.0);\n\
			float vBloomOffsets[9];\n\
			vBloomOffsets[0] = uVBloomOffsets[0][0];\n\
			vBloomOffsets[1] = uVBloomOffsets[0][1];\n\
			vBloomOffsets[2] = uVBloomOffsets[0][2];\n\
			vBloomOffsets[3] = uVBloomOffsets[1][0];\n\
			vBloomOffsets[4] = uVBloomOffsets[1][1];\n\
			vBloomOffsets[5] = uVBloomOffsets[1][2];\n\
			vBloomOffsets[6] = uVBloomOffsets[2][0];\n\
			vBloomOffsets[7] = uVBloomOffsets[2][1];\n\
			vBloomOffsets[8] = uVBloomOffsets[2][2];\n\
			float vBloomWeights[9];\n\
			vBloomWeights[0] = uVBloomWeights[0][0];\n\
			vBloomWeights[1] = uVBloomWeights[0][1];\n\
			vBloomWeights[2] = uVBloomWeights[0][2];\n\
			vBloomWeights[3] = uVBloomWeights[1][0];\n\
			vBloomWeights[4] = uVBloomWeights[1][1];\n\
			vBloomWeights[5] = uVBloomWeights[1][2];\n\
			vBloomWeights[6] = uVBloomWeights[2][0];\n\
			vBloomWeights[7] = uVBloomWeights[2][1];\n\
			vBloomWeights[8] = uVBloomWeights[2][2];\n\
		    for( int i = 0; i < 9; i++ )\n\
		    {\n\
		        color += (texture2D( uSampler, vTexCoord + vec2( 0.0, vBloomOffsets[i] ) ) * vBloomWeights[i] );\n\
		    }\n\
		    gl_FragColor = vec4( color.rgb, 1.0 );\n\
		}\n\
		";
		
		return fragStr;
	}
}
);